package com.xinghai.admin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.xinghai.admin.domain.SysNoticeRead;
import com.xinghai.admin.mapper.SysNoticeReadMapper;
import com.xinghai.admin.service.ISysNoticeReadService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 消息通知查看Service业务层处理
 *
 * @author xinghai
 * @date 2025-06-23
 */
@Service
public class SysNoticeReadServiceImpl extends MPJBaseServiceImpl<SysNoticeReadMapper, SysNoticeRead>
        implements ISysNoticeReadService
{

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    /**
     * 查询消息通知查看列表
     *
     * @param sysNoticeRead 消息通知查看
     * @return 消息通知查看集合
     */
    @Override
    public List<SysNoticeRead> list(SysNoticeRead sysNoticeRead) {
        QueryWrapper<SysNoticeRead> queryWrapper = new QueryWrapper<>();
        if (sysNoticeRead.getNoticeId()!= null ){
            queryWrapper.eq("notice_id", sysNoticeRead.getNoticeId());
        }
        if (sysNoticeRead.getUserId()!= null ){
            queryWrapper.eq("user_id", sysNoticeRead.getUserId());
        }
        if (sysNoticeRead.getReadStatus()!= null  && !sysNoticeRead.getReadStatus().isEmpty()){
            queryWrapper.eq("read_status", sysNoticeRead.getReadStatus());
        }
        if (sysNoticeRead.getParams().get("beginCreateTime")!= null && !"".equals(sysNoticeRead.getParams().get("beginCreateTime"))
        &&sysNoticeRead.getParams().get("endCreateTime")!= null && !"".equals(sysNoticeRead.getParams().get("endCreateTime"))){
            queryWrapper.between("create_time", sysNoticeRead.getParams().get("beginCreateTime"),sysNoticeRead.getParams().get("endCreateTime"));
        }
        return this.list(queryWrapper);
    }

    /**
     * 导入消息通知查看信息
     *
     * @param list 消息通知查看信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<SysNoticeRead> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            SysNoticeReadMapper sysNoticeReadMapper = sqlSession.getMapper(SysNoticeReadMapper.class);
            list.forEach(sysNoticeReadMapper::insert);
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }
}
